package com.penghu.utils;

import org.json.JSONObject;

/**
 * @author Mall
 * @category 返回工具类
 */
public class ResultList {
    // 成功返回码
    public static final int SUCCESS_CODE = 0;
    // 失败返回码
    public static final int ERROR_CODE = 1;
    //登陆权限返回码
    public static final int AUTH_ERROR_CODE = -1;

    private int code; // 返回编码
    private String message; // 返回内容
    private Object data; // 返回数据
    private long total; // 总数

    public ResultList() {
    }

    public ResultList(Object data) {
        this.code = SUCCESS_CODE;
        this.message = "成功";
        this.data = data;
    }

    public ResultList(Object data, long total, int type) {

        if (type == SUCCESS_CODE) { // 成功
            this.code = SUCCESS_CODE;
            this.message = "成功";
        } else {
            this.code = ERROR_CODE;
            this.message = "失败";
        }
        this.data = data;
        this.total = total;
    }

    public ResultList(int code, String message, Object data, long total) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total = total;
    }

    /// 失败
    public ResultList error(String message) {
        this.code = ERROR_CODE;
        this.message = message;
        return this;
    }


    /// 失败
    public ResultList errorAuth(String message) {
        this.code = AUTH_ERROR_CODE;
        this.message = message;
        return this;
    }


    /// 失败
    public ResultList error(String message, Object data) {
        this.code = ERROR_CODE;
        this.message = message;
        this.data = data;
        return this;
    }

    // 成功
    public ResultList success(String message) {
        this.code = SUCCESS_CODE;
        this.message = message;
        return this;
    }

    // 成功
    public ResultList success(Object data) {
        this.code = SUCCESS_CODE;
        this.message = "成功";
        this.data = data;
        return this;
    }

    // 成功
    public ResultList success(Object data, long total) {
        this.code = SUCCESS_CODE;
        this.message = "成功";
        this.data = data;
        this.total = total;
        return this;
    }

    // 成功
    public ResultList success(String message, Object data, long total) {
        this.code = SUCCESS_CODE;
        this.message = message;
        this.data = data;
        this.total = total;
        return this;
    }

    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("message", message);
        jsonObject.put("data", data);
        jsonObject.put("total", total);
        return jsonObject.toString();
    }

    /**
     * 封装自动
     */
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }
}
